#!/usr/bin/python
#encoding=utf-8

'''
Created on May 19, 2014

@author: root
'''
import time
from datetime import datetime
from wm.db.TransactionManager import TransactionManager
from wm.lib.emaillib import sendMail
from wm.etc.config import ALARMTYPE
from wm.lib import emailalarm
from wm.lib.jpushutil import jpushalarm


def website_alert(**items):
    tran = TransactionManager()
    try:
        tran.startTransaction()
        sql = "select ms.stfcellphone,ms.stfemail,ms.stfname from maintenancestaffandweb as msw,websiteconfiginfo as wc,maintenancestaff as ms where msw.maintenanceStaff_id = ms.id and msw.websiteConfigInfo_id = wc.id and wc.id ='"+str(items['websiteid'])+"'"
        mslist = tran.select(sql)
        tran.commitTransaction()
    except:
        tran.rollbackTransaction()
        return ''
    finally:
        tran.close()
       
        for ms in mslist:
            mail_to = ms[1]
            phone = ms[0]
            m_subject = None
            m_content = None
            if items['alarm_type'] == ALARMTYPE['PORT_STATUS_ALARM']:
                m_subject,m_content = emailalarm.tcp_alarm_content(items['websitename'], items['content'], items['url'])
            elif items['alarm_type'] == ALARMTYPE['PAGE_TAMPER_ALARM']:
                m_subject,m_content = emailalarm.tamper_alarm_content(items['websitename'], items['content'], items['url'])
            elif items['alarm_type'] == ALARMTYPE['VULNERABILITY_ALARM']:
                m_subject,m_content = emailalarm.vul_alarm_content(items['websitename'], items['content'], items['url'])  
            elif items['alarm_type'] == ALARMTYPE['SENSITIVE_WORDS_ALARM']:
                m_subject,m_content = emailalarm.senword_alarm_content(items['websitename'], items['content'], items['url']) 
            sendMail(m_subject, m_content, mail_to)
            
            extras = {}
            extras['websitename'] = items['websitename']
            extras['content'] = items['content']
            extras['url'] = items['url']
            extras['date'] = time.strftime('%Y-%m-%d',time.localtime(time.time()))
            jpushalarm.sendMessage(phone, items['content'], items['websitename'],extras={})
            

            
            
if __name__=='__main__':
    print '------------------'
    items = {}
    items['alarm_type'] = 2
    items['websitename'] = '杭州市人民政府'
    items['content'] = '敏感：反共'
    items['url'] = 'http://www.zjut.edu.cn'
    
    mail_to = "pez1420@163.com"
    phone = '15068822927'
    '''
    m_subject = None
    m_content = None
    if items['alarm_type'] == ALARMTYPE['PORT_STATUS_ALARM']:
        m_subject,m_content = emailalarm.tcp_alarm_content(items['websitename'], items['content'], items['url'])
    elif items['alarm_type'] == ALARMTYPE['PAGE_TAMPER_ALARM']:
        m_subject,m_content = emailalarm.tamper_alarm_content(items['websitename'], items['content'], items['url'])
    elif items['alarm_type'] == ALARMTYPE['VULNERABILITY_ALARM']:
        m_subject,m_content = emailalarm.vul_alarm_content(items['websitename'], items['content'], items['url'])  
    elif items['alarm_type'] == ALARMTYPE['SENSITIVE_WORDS_ALARM']:
        m_subject,m_content = emailalarm.senword_alarm_content(items['websitename'], items['content'], items['url'])  
    
    sendMail(m_subject, m_content, mail_to)
    print '-----------111111111111'
    '''
    extras = {}
    extras['websitename'] = items['websitename']
    extras['content'] = items['content']
    extras['url'] = items['url']
    extras['date'] = time.strftime('%Y-%m-%d',time.localtime(time.time()))
    
    jpushalarm.sendMessage(phone, items['content'], items['websitename']+' '+items['url'],extras=extras)
    